<template>
	<view>
		<view style="width: 750upx;height: 176upx;">
			<image src="../../static/shangmen/bj.png" style="width: 750upx;height: 176upx;position: absolute;"></image>
			<view style="width: 750upx;height: 176upx;text-align: center;font-size: 36upx;line-height: 176upx;position: absolute;z-index: 1;color: #fff;">购物车</view>
		</view>
		<view class="shop-car">
		    <commonCar v-if="flag" :list="goodsProducts" @delect="delect" @subData="subData" @addData="addData" @accounts="accounts"> </commonCar>
			<!-- <commonCar v-if="flag" :list="goodsProducts" @delect="delect" @subData="subData" @addData="add" @accounts="accounts"> </commonCar> -->
		</view>
	</view>
</template>

<script>
    import commonCar from '@/components/shopCar/shopCar.vue'
    export default {
        data() {
            return {
				data:[],
				flag: false,
				z_money:0,
				goodsProducts:[],
                //checked 1为未选择 2为选中
                goodsProducts: [{
                    // name: '华为旗舰店',
                    // checked: 1,
                    goods: [{
                        img: '../../static/10.png',
                        title: '严选平口纸箱定制包装成就品牌全品类高效率',
                        remark: '规格:严选平口纸箱  12#（400个）x1',
                        price: '128.80',
                        number: 1,
                        checked: 1
                    }, {
                        img: '../../static/10.png',
                        title: 'mate40',
                        remark: '128G',
                        price: '116.80',
                        number: 1,
                        checked: 1
                    }]
                }],
            }
        },
		onLoad() {
			uni.setNavigationBarColor({
				frontColor:"#ffffff",
				backgroundColor:"#cf4052",
			})
		},
		onShow() {
			this.flag = false
			this.cart_list_api()
		},
        methods: {
			add(params,number) {
				console.log('numbera',number)
			},
			//数量+
			addData(params,number) {
				var that = this
				var numbera = Number(number)
				that.$request(that.$Api.cart_number_api, {
					access_token:uni.getStorageSync('access_token'),
					cart_id:params,
					number:numbera,
				},"POST").then(res => {
					that.notice = res.data.data.notice
				})
			},
			//数量-
			subData(params,number) {
				var that = this
				var numbera = Number(number)
				that.$request(that.$Api.cart_number_api, {
					access_token:uni.getStorageSync('access_token'),
					cart_id:params,
					number:numbera,
				},"POST").then(res => {
					console.log('numbera',numbera)
					that.notice = res.data.data.notice
				})
			},
			//购物车列表
			cart_list_api(goods_id) {
				var that = this
				that.$request(that.$Api.cart_list_api, {
					access_token:uni.getStorageSync('access_token'),
				},"POST").then(res => {
					var goodss = []
					var da = []
					for( let i=0;i<res.data.data.list.length;i++ ) {
						var data = {
							img: res.data.data.list[i].image,
							title: res.data.data.list[i].name,
							// remark: res.data.data.list[i].specification.material+res.data.data.list[i].specification.size,
							price: res.data.data.list[i].price || res.data.data.list[i].money,
							number: res.data.data.list[i].number,
							checked: 1,
							cart_id: res.data.data.list[i].cart_id,
							type: res.data.data.list[i].type,
							user_id: res.data.data.list[i].user_id,
							goods_id: res.data.data.list[i].goods_id,
							specification:res.data.data.list[i].specification,
						}
						da.push(data)
					}
					var goods = [{goods:da}]
					that.goodsProducts = goods
					console.log('that.goodsProducts',that.goodsProducts)
					that.flag = true
				})
			},
            delect(e,isCut){
				console.log('11111',e,isCut)
				if( isCut ){
					var goods = [] 
					var z_money = 0
					for( let i=0;i<e[0].goods.length;i++ ){
						if(e[0].goods[i].checked != 1){
							goods.push(e[0].goods[i])
							z_money = Number(z_money) + Number(Number(e[0].goods[i].price)*Number(e[0].goods[i].number))
						}
					}
					this.z_money = Number(z_money)
					this.cart_page_api(goods)
				}else{
					var cart_id = []
					for( let i=0;i<e[0].goods.length;i++ ){
						if(e[0].goods[i].checked != 1){
							cart_id.push(e[0].goods[i].cart_id)
						}
					}
					this.cart_delete_api(cart_id)
				}
            },
			//删除
			cart_delete_api(cart_id) {
				var that = this
				that.$request(that.$Api.cart_delete_api, {
					access_token:uni.getStorageSync('access_token'),
					cart_id:JSON.stringify(cart_id),
				},"POST").then(res => {
					that.flag = false
					that.cart_list_api()
				})
			},
            //立即下单页面	
			cart_page_api(goods) {
				console.log('goods',goods)
				// let data_list = {
				// 	name:this.data_list.title,
				// 	image:this.data_list.image,
				// 	price:this.data_list.price,
				// 	integral:this.data_list.integral,
				// 	type:this.data_list.type,
				// 	goods_id:this.data_list.goods_id,
				// }
				uni.navigateTo({
					url:'../order_zfs/order_zfs?data_list='+JSON.stringify(goods)+'&z_money='+this.z_money
				})
				// var data = {
				// 	goods_id:that.goods_id,
				// 	specification:JSON.stringify(that.specification_s),
				// 	number:that.quantity,
				// 	money:that.price,
				// }
				// uni.navigateTo({
				// 	url:'../../shopping/submit_order_general/submit_order_general?goods_id='+that.goods_id+'&number='+data.number+'&money='+data.money+'&size='+that.specification_s.size+'&material='+that.specification_s.material
				// })
			},
			accounts(e){
                console.log('32---1',e);
            }
        },
		components: {
		    commonCar
		},
    }
</script>

<style lang="scss" scoped>
    .shop-car {
        width: 750rpx;
        min-height: 100vh;
        display: flex;
        flex-direction: column;
        align-items: center;
        background: #F5F5F5;
		padding-top: 30upx;
    }
</style>